using M13.Base;
using QFramework;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.UI;

namespace M13.Game.UI
{
    public class HUD : MonoBehaviour, IController, ICanSendEvent
    {
        [FormerlySerializedAs("_shortBarObj")] [SerializeField] private GameObject shortBarObj;

        [FormerlySerializedAs("_shortcutSlot")] [SerializeField] private GameObject[] shortcutSlot = new GameObject[9];

        [FormerlySerializedAs("_slotSelectBox")] [SerializeField] private GameObject slotSelectBox;

        private void Awake()
        {
            shortBarObj = transform.Find("ShortBar").gameObject;
            slotSelectBox = shortBarObj.transform.Find("Slot_Select_Box").gameObject;
            for (int i = -0; i < 9; i++)
            {
                shortcutSlot[i] = shortBarObj.transform.Find($"Slot_{i}").gameObject;
            }
        }

        private void Start()
        {
            
            
            
        }

        public void SetSlotSelectBoxPos(int index)
        {
            slotSelectBox.transform.position = shortcutSlot[index].transform.position;
        }

        public void SetShortcutSlot(int index, Sprite itemIcon, int itemCount, float normalizedDurability)
        {
            GameObject slot = shortcutSlot[index];
            Image image = slot.transform.Find("ItemIcon").GetComponent<Image>();
            image.color = itemIcon == null ? new Color(1f, 1f, 1f, 0f) : new Color(1f, 1f, 1f, 1f);
            image.sprite = itemIcon;
            slot.transform.Find("ItemCount").GetComponent<Text>().text = (itemCount == 0 || itemCount == 1) ? null : itemCount.ToString();

            GameObject durableDisplay = slot.transform.Find("DurableDisplay").gameObject;
            if (normalizedDurability <= 0f)
            {
                durableDisplay.SetActive(false);
            }
            else
            {
                durableDisplay.SetActive(true);
                durableDisplay.GetComponent<Slider>().value = normalizedDurability;
            }
        }

        public IArchitecture GetArchitecture()
        {
            return GameArchitecture.Interface;
        }
    }
}